package cn.tedu.web.controller;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.tedu.web.entity.Category;
import cn.tedu.web.mapper.CategoryMapper;
import cn.tedu.web.service.ICategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 前端控制器
 * </p>
 * category
 * 
 * @author issen
 * @since 2021-09-23
 */
@SuppressWarnings("all")
@RestController
@Api(value = "Category", tags = { "Category" }, description = "商品分类")
@RequestMapping("/api/category")
public class CategoryController {

    @Autowired
    public ICategoryService categoryService;
    @Autowired
    private CategoryMapper categoryMapper;

    @PostMapping("add")
    @ApiOperation(value = "添加分类")
    public Integer add(@RequestBody JSONObject param) {
        String name = param.getString("name");

        return categoryService.save(name);
    }
    @PostMapping("del")
    @ApiOperation(value = "删除分类")
    public ResponseEntity<Object> del(@RequestBody Category category) {
        Integer status = categoryService.delete(category);
        if(status <= 0){
            return new ResponseEntity<>("当前分类无法删除",HttpStatus.OK);
        }else{
            return new ResponseEntity<>("删除完成",HttpStatus.OK);
        }
    }

    @GetMapping("list")
    @ApiOperation(value = "查询所有分类")
    public List<Category> List() {
        return categoryService.findAll();
    }
 
    @ResponseBody
    @ApiOperation(value = "获取分类列表")
    @RequestMapping("info")
    public ResponseEntity<Object> list(@RequestBody JSONObject param) {
        Long current = (long) 1;
        Long size = (long) 10;
        current = Long.parseLong(param.getString("current"));
        size = Long.parseLong(param.getString("size"));
        // System.out.println(param.getString("current"));
        return new ResponseEntity<>(categoryMapper.selectPage(new Page<>(current, size), null), HttpStatus.OK);
    }
}
